<?php
/**
 *NewProduct Controller
 *
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 * Class NewProduct_controller
 * Sets up a new product
 *@package controller
 */
class NewProduct_Controller extends Controller{

    /**
     * language in wich to show page
     * @var integer
     */
    private $iLang ='';

    /**
     * Reference to view of content section
     * @var object
     */
    private $oContentView;

    /**
     * Reference to model that retrieves page data from db
     * @var object
     */
    private $oContentModel;

    /**
     * Reference to model that retrieves page navigation elements from db
     * @var object
     */
    private $oNavigationModel;

    /**
     * Reference to view of navigation section
     * @var object
     */
    private $oNavigationView;

    /**
     * Reference to view of left navigation section in cms, its shows the entire pagestructure
     * @var object
     */
    private $oLeftNavigationView;
    /**
     * Stores the category ID in which the product should be created
     * @var integer
     */
    private $iCategory="";

    public function __construct(){
        //get Viewmanger reference from parent constructor
        parent::__construct();
        
        //load models and views
        $this->oProductModel = Loader::loadModel('newProduct');
        $this->oNavigationModel = Loader::loadModel('navigation');
        $this->oNavigationView = $this->oViewmanager->getView('navigation');
        $this->oLeftNavigationView = $this->oViewmanager->getView('leftnavigation');

        //Check language set in Cookie, take default language specified in config if not set
        if (isset($_COOKIE['language'])){
            $this->iLang = intval($_COOKIE['language']);
        }else{
            $this->iLang = DEFAULT_LANGUAGE;
        }
    }
    
    /**
     * checks GET-Parameters forwarded from Router::route
     * @param Array $aArgs Arguments to check
     */
    public function checkArgs($aArgs){
        //Set picture upload to false
        $bPictureUpload= FALSE;

        //If category is set save the value in iCategory
        if(isset($_GET['ca'])){
            $this->iCategory = $_GET['ca'];
            //If the method addNewProduct is called and successfully executed set bPictureUpload to true
            if(in_array("addNewProduct",$aArgs)){
                $bPictureUpload = $this->oProductModel->addProduct($_POST, $this->iCategory, $this->iLang);
            }
        }
        //If true load the view to upload an image
        if($bPictureUpload){
            $this->oContentView = $this->oViewmanager->getView("newProductImage");
        }else{
            $this->oContentView = $this->oViewmanager->getView("newProduct");
        }
    }
/**
 * Executes controller methods
 * @param array different arguments
 */
    public function execute($aArgs){


        $this->checkArgs($aArgs);
        //Gets page data from database and passes data to views
        $this->oNavigationView->assign('contentANDproductPages', $this->oNavigationModel->getPageStructure('NULL', $this->iLang, "(type = 'contentPage' OR type = 'product')"));
        $this->oNavigationView->assign('products', $this->oNavigationModel->getPagesByTyp('', $this->iLang, 'product'));
        $this->oNavigationView->assign('categories', $this->oNavigationModel->getPagesByTyp('', $this->iLang, 'category'));
        $this->oLeftNavigationView->assign('leftnavigation', $this->oNavigationModel->getPageStructure('NULL', $this->iLang));
        //Insert data in the correct sections of the website layout
        $this->oViewmanager->setSection('Title', $this->oProductModel->getPageTitle($this->iCategory, $this->iLang));
        $this->oViewmanager->setSection('Content', $this->oContentView->display());
        $this->oViewmanager->setSection('LeftNavigation', $this->oLeftNavigationView->display());
        $this->oViewmanager->setSection('Navigation', $this->oNavigationView->display());
    }

}
